package ru.nsu.ccfit.shumkina.algorithm.logworks;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Вера
 * Time: 20:03
 * To change this template use File | Settings | File Templates.
 */
public class Logs {
    // todo: getters setters so on
    public ArrayList<String> logFilesNames;
    private int currentFileId = 0;
    BufferedReader currFile = null;

    public Session getNextSession() {
        String userId = null;
        ArrayList<String> path = new ArrayList<String>();

        try {
            if (currFile == null) {
                if (currentFileId >= logFilesNames.size()) {
                    return null;
                }
                currFile = new BufferedReader(new FileReader(logFilesNames.get(currentFileId++)));
            }
            String line = currFile.readLine();
            if (line == null)
            {
                currFile = null;
                return getNextSession();
            }
            String[] nodes = line.split(",");

            int idx = 0;
            while (idx < nodes.length){
                if (idx == 0){
                    idx++;
                    userId = nodes[0];
                    continue;
                }
                // this statement used to clear new format logs from unneeded info, makes them like old logs
                if (nodes[idx].equals("true") || nodes[idx].equals("false") || nodes[idx].equals("") ||
                        nodes[idx].equals("ug") || (nodes[idx].charAt(0) <= '9' && nodes[idx].charAt(0) >= '0') ||
                        nodes[idx].charAt(0) == ' ' || nodes[idx].charAt(0) == '#' || nodes[idx].charAt(0) == '*'
                        || nodes[idx].charAt(0) == '+') {
                    idx++;
                    continue;
                }
                path.add(nodes[idx]);
                idx++;
            }
        } catch (IOException e) {
            e.printStackTrace();  //todo: To change body of catch statement use File | Settings | File Templates.
        }
        if (userId == null)
            return null;
        return new Session(userId, path);
    }
}
